import axios from 'axios';
import qs from 'qs';
import { Toast } from 'vant';
let http = {
    rootUrl:'http://10.0.0.24:80',//http://work.073c.com',
    // _id:'5bf4d78b1340502f4c357f71',
    // token:'0test0',
    // _id:'5bf4d78b1340502f4c357f75',
    // token:'1test1',
    // _id:'5bf4d78b1340502f4c357f77',
    // token:'2',
    _id:'5bf4d78b1340502f4c357f7a',
    token:'3',
};
http.get=function(router,params,cb){
    axios.get(`${this.rootUrl}${router}`, {params: params}).then(res=>{
        cb(res.data);
    }).catch(function (error) {
        Toast(error.message);
    });
};
http.post=function(router,params,cb){
    axios.post(`${this.rootUrl}${router}`, qs.stringify(params), {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(res=>{
        cb(res.data);
    }).catch(function (error) {
        //console.error(error);
        Toast(error.message);
    });
};
//自动处理服务端post返回的code数据
http.postJsonData=function(router,params,cb){
    this.post(router,params,(data)=>{
        if(data.code===200){
            cb(data.data);
        }else{
            Toast(data.msg);
        }
    });

};
//自动处理服务端get返回的code数据
http.getJsonData=function(router,params,cb){
    this.get(router,params,(data)=>{
        if(data.code===200){
            cb(data.data);
        }else{
            Toast(data.msg);
        }
    });
};
http.postUserJsonData=function(router,params,cb){
    this.postJsonData(router,Object.assign(params,{_id:this._id,token:this.token}),cb);
};

http.getUserJsonData=function(router,params,cb){
    this.getJsonData(router,Object.assign(params,{_id:this._id,token:this.token}),cb);
};
export default http;
